﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Management;
using System.Diagnostics;

namespace TestBatchPrint
{
    [TestClass]
    public class UnitTest1
    {

        public const string TEST_DIR = @"C:\Users\my\Documents\Visual Studio 2015\Projects\BatchPrintHelper\TestBatchPrint\TestDocs";
        [TestMethod]
        [ExpectedException(typeof(System.Runtime.InteropServices.COMException))]
        public void OpenDocInWrongApp()
        {
            var app = new Word.Application();
            try
            {

            app.Visible = true;
                var excelFile = Directory.GetFiles(TEST_DIR, "*.xls*")[0];
            var doc = app.Documents.OpenOld(excelFile);
                doc.PrintOut();
                doc.Close();
            }
            finally{
                app.Quit();
            }

        }

        [TestMethod]
        public void TestExcelSetActivePrinter()
        {
            var app = new Excel.Application();
            try
            {
                var excelFile = Directory.GetFiles(TEST_DIR, "*.xls*")[0];
                var workbook = app.Workbooks.Open(excelFile);

                app.ActivePrinter = "Microsoft Print to PDF 在 Ne01:";
                workbook.PrintOutEx();
             
            }
            finally
            {
                app.Quit();
            }
        }

        [TestMethod]
        public void TestWordAppQuit()
        {
            for(var i = 0; i < 100; i++)
            {

                Console.WriteLine($"the {i} times.");

                System.Diagnostics.Debug.WriteLine($"the {i} times.");
            var app = new Word.Application();
            app.Quit();
            }
        }
        [TestMethod]
        public void TestGetScannerNames()
        {

            ManagementObjectSearcher printers = new ManagementObjectSearcher("Select Name from Win32_Printer");

            foreach (ManagementObject printer in printers.Get())
              Trace.WriteLine($"Name: {printer.GetPropertyValue("Name")}" );
        }


        [TestMethod]
        public void TestExcelSetActivePrinter1()
        {
            var app = new Excel.Application();
            try
            {
                var excelFile = Directory.GetFiles(TEST_DIR, "*.xls*")[0];
                var workbook = app.Workbooks.Open(excelFile);

                //app = "Microsoft Print to PDF";
                try {
                    app.ActivePrinter = "Microsoft Print to PDF on Ne01:";
                }
                catch
                {
                    app.ActivePrinter = "Microsoft Print to PDF 在 Ne01:";
                }
                workbook.PrintOutEx();

            }
            finally
            {
                app.Quit();
            }
        }


        [TestMethod]
        public void TestXLSM()
        {
            var app = new Excel.Application();
            try
            {
                var excelFile = Directory.GetFiles(TEST_DIR, "*.xlsm")[0];
                var workbook = app.Workbooks.Open(Filename:excelFile
                    //,
                    //IgnoreReadOnlyRecommended:true,
                    //CorruptLoad:1

                    );
                //app = "Microsoft Print to PDF";
                
                try
                {
                    app.ActivePrinter = "Microsoft Print to PDF on Ne01:";
                }
                catch
                {
                    app.ActivePrinter = "Microsoft Print to PDF 在 Ne01:";
                }

                (workbook.ActiveSheet as Excel.Worksheet).PageSetup.PrintComments = Excel.XlPrintLocation.xlPrintNoComments;
                workbook.PrintOutEx();
                workbook.Close(SaveChanges: false);

            }
            finally
            {
                app.Quit();
            }
        }
    }
}
